package game.efficientCollisionPrediction;

import java.util.ArrayList;
import java.util.Collection;

import game.SpaceObject;

public class FutureEntityCollision extends FutureCollision {

	private final SpaceObject object1;
	private final SpaceObject object2;
	
	public FutureEntityCollision(SpaceObject object1, SpaceObject object2) throws IllegalStateException {
		this.object1 = object1;
		this.object2 = object2;
		if(isTerminated()) 
			throw new IllegalStateException();
		
		update();
	}
	
	@Override
	public double getTimeToCollision() {
		if(isTerminated())
			return Double.POSITIVE_INFINITY;
		return getObject1().getTimeToCollision(getObject2());
	}

	@Override
	public Collection<SpaceObject> getInvolvedSpaceObjects() {
		Collection<SpaceObject> list= new ArrayList<SpaceObject>();
		list.add(getObject1());
		list.add(getObject2());
		return list;
	}
	
	public SpaceObject getObject1() {
		return object1;
	}

	public SpaceObject getObject2() {
		return object2;
	}
	
	public boolean isTerminated() {
		if(getObject2() == null)
			return getObject1().isTerminated();
		else
			return getObject1().isTerminated() ||  getObject2().isTerminated();
	}
	
	public void collide() {
		getObject1().collideWith(getObject2());
	}
}
